﻿using System.IO;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using ReactiveUI;
using ScienceAppService.ModuleInterface;
using Microsoft.Win32;
using CommonEntity.CommonImageEntities;
using ReactiveUI.Fody.Helpers;
using ScienceAppService.ModuleService;
using Autofac;
using ScienceAIApp.ViewModels.AlertViewModel;

namespace ScienceAIApp.ViewModels;
public class SeriesDescriptionViewModel : ScienceBaseViewModel
{
    private ISplitService _splitService;
    private IImageShowService _imageShowService;
    private IMarkDrawService _markDrawService;

    [Reactive] public string CurrentSeriesName { get; set; }

    /// <summary>
    /// 打开文件
    /// </summary>
    public ReactiveCommand<Unit, Unit> OpenFolderCommand { get; set; }
    public ReactiveCommand<Unit, Unit> ShowHistoryRecordCommand { get; set; }

    /// <summary>
    /// 打开序列选择弹窗
    /// </summary>
    public Interaction<IList<SplitSeriesInfo>, Unit> ChooseDicomSeriesInteraction { get; set; }


    public SeriesDescriptionViewModel(ISplitService splitService, IImageShowService imageShowService, IMarkDrawService markDrawService)
    {
        _splitService = splitService;
        _imageShowService = imageShowService;
        _markDrawService = markDrawService;

        _imageShowService.FrushSeriesDescriptionEvent += FrushSeriesDescriptionHandler;

        ChooseDicomSeriesInteraction = new Interaction<IList<SplitSeriesInfo>, Unit>();

        // 历史病人列表展示
        ShowHistoryRecordCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            await using var scope = AppIoc.AppContainer.BeginLifetimeScope();
            var recordVm = scope.ResolveOptional<HistoryRecordViewModel>()!;
            await recordVm.InitialHistory();
            AppEnvironment.Instance.AlertWindow.Show();
            AppEnvironment.Instance.AlertWindow.SetAlertViewModel(recordVm);
        });

        OpenFolderCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter = "All files (*.*)|*.*|Dicom files (*.dcm)|*.dcm"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                var selectedFilePath = openFileDialog.FileName;

                // todo 特殊处理nii.gz 此时不需要展示序列选择了
                if (selectedFilePath.Contains("nii.gz") || selectedFilePath.Contains("mha"))
                {
                    var dir = Path.GetDirectoryName(selectedFilePath);
                    await _imageShowService.SetNiiGzFile(dir!);
                    _markDrawService.ClearData();
                    await _imageShowService.SetImageInfo();
                    await _imageShowService.GetTagInfoMat();
                }
                else
                {
                    var dicomPath = Path.GetDirectoryName(selectedFilePath);

                    var siList = await _splitService.SplitDicom(dicomPath);

                    if (siList != null && siList?.Count > 0)
                    {
                        await ChooseDicomSeriesInteraction!.Handle(siList);
                    }
                }
            }
        });

    }


    public void FrushSeriesDescriptionHandler(string seriesDescription)
    {
        RxApp.MainThreadScheduler.Schedule(() =>
        {
            CurrentSeriesName = seriesDescription;
        });
    }








}
